<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  $msg = 0;
  $partyArray = array();
  if(isset($_POST['submitBtn']))
  {
  	$loanDate     = $_POST['loanDateYear']."-".$_POST['loanDateMonth']."-".$_POST['loanDateDay'];
  	$amount       = isset($_POST['amount']) ? $_POST['amount'] : 0;
  	$interst      = isset($_POST['interst']) ? $_POST['interst'] : 0;
  	$givenAmount  = isset($_POST['givenAmount']) ? $_POST['givenAmount'] : 0;
  	$partyId      = isset($_POST['partyId']) ? $_POST['partyId'] : 0;
  	
  	if($_POST['partyId'] == 0)
  	{
  		$insertNewParty = "INSERT INTO party(partyName)values('".$_POST['partyName']."')";
  		$insertNewPartyRes = mysql_query($insertNewParty);
  		$partyId = mysql_insert_id();
  	}
  	
  	
    $insertParty =  "INSERT INTO loan(partyId,loanDate,amount,interst,givenAmount)
  	                               VALUES(".$partyId.",'".$loanDate."',".$amount.",".$interst.",".$givenAmount.")";
  	$insertPartyRes = mysql_query($insertParty);
  	if(!$insertPartyRes)
  	{
  		echo "Insert Fail";
  	}
  	else
  	{
  		header("party.php");
  	}
  }
  $i=0;
  $selectParty = "SELECT partyId,partyName
                     FROM party";
  $selectPartyRes = mysql_query($selectParty);
  while($partyRow = mysql_fetch_array($selectPartyRes))
  {
  	$partyArray['partyId'][$i]   = $partyRow['partyId'];
  	$partyArray['partyName'][$i] = $partyRow['partyName'];
  	$i++;
  }
  include("./bottom.php");
  $smarty->assign("partyArray",$partyArray);
  $smarty->assign("msg",$msg);
  $smarty->display("loan.tpl");
}  
?>